package demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//用锁保护Bank类的transfer方法
public class Bank {
    //先创建锁对象
    private Lock bankLock=new ReentrantLock();
    public void transfer(int from,int to,int amount){
        //上锁
        bankLock.lock();
        try{
            System.out.println(Thread.currentThread());
            System.out.println("Bank");
        }finally{
            //解锁，不然就会阻塞
            bankLock.lock();
        }


    }
}
